package com.zijiebutiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zijiebutiao.mapper.TMemberMapper;
import com.zijiebutiao.mapper.TSetmealMapper;
import com.zijiebutiao.pojos.TMember;
import com.zijiebutiao.pojos.TOrder;
import com.zijiebutiao.pojos.TSetmeal;
import com.zijiebutiao.service.TOrderService;
import com.zijiebutiao.mapper.TOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
* @author ZhangQingTian
* @description 针对表【t_order】的数据库操作Service实现
* @createDate 2023-03-06 09:52:22
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder>
    implements TOrderService{
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TSetmealMapper tSetmealMapper;
    @Override
    public boolean creatOrder(Integer id,Map map) throws ParseException {

        if (map.get("sex")==null||map.get("orderDate")==null||map.get("phoneNumber")==null){
            return false;
        }
        String sex="男".equals(map.get("sex"))?"1":"2";
        LambdaQueryWrapper<TMember> wq= Wrappers.lambdaQuery();
        wq.eq(TMember::getPhonenumber,map.get("phoneNumber"));
        wq.eq(TMember::getName,map.get("name"));
        wq.eq(TMember::getSex,sex);

        TMember tMember = tMemberMapper.selectOne(wq);
        if (tMember==null){
            return false;
        }

        TSetmeal tSetmeal = tSetmealMapper.selectById(id);
        String setmealSex = tSetmeal.getSex();
        String sexs = "男".equals(map.get("sex")) ? "1" : "2";
        if (!"0".equals(setmealSex)){
            if (!setmealSex.equals(sexs)){
                return false;
            }
        }
        TOrder tOrdersql=tOrderMapper.selectOne(
                Wrappers.lambdaQuery(TOrder.class)
                        .eq(TOrder::getOrderdate,map.get("orderDate"))
                        .eq(TOrder::getSetmealId,id));
        if (tOrdersql!=null){
            return false;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(new Date());
        String data2= (String) map.get("orderDate");
        if (date1.compareTo(data2) > 0) {
            return false;
        }
        TOrder tOrder=new TOrder();
        tOrder.setMemberId(tMember.getId());
        tOrder.setSetmealId(id);
        tOrder.setOrdertype("后台预约");
        tOrder.setOrderstatus("未到诊");
        Date date=format.parse((String) map.get("orderDate"));

        tOrder.setOrderdate(date);
        tOrderMapper.insert(tOrder);
        return true;
    }
}




